package com.vf.s.mvc.biz.controller;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.vf.core.controller.BaseController;
import com.vf.core.util.UuidUtil;
import com.vf.s.common.model.sync.BizSyncDataLog;
import com.vf.s.common.model.sync.BizSyncDataTask;

public class BizSynDataContoller extends BaseController {
	public void index() {
		render("list.html");
	}

	public void listData() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("limit", 10);
		String keyword = getPara("keyword");
		String select = " select *  ";
		String sqlExceptSelect = " from " + BizSyncDataTask.TABLE_NAME + " o  where 1=1 ";
		if (!StrKit.isBlank(keyword)) {
			sqlExceptSelect += " and o.name like '%" + keyword + "%'  ";
		}

		Page<BizSyncDataTask> page = BizSyncDataTask.dao.paginate(pageNumber, pageSize, select, sqlExceptSelect);
		renderLayuiPage(0, page.getList(), "", page.getTotalRow());
	}

	/***
	 * 保存
	 */
	public void save() {
		BizSyncDataTask role = getModel(BizSyncDataTask.class);
		if (StrKit.notBlank(role.getId())) {
			if (role.update()) {
				renderSuccess("更新成功！");
			} else {
				renderError("更新失败！");
			}
		} else {
			role.setId(UuidUtil.getUUID());
			if (role.save()) {
				renderSuccess("保存成功！");
			} else {
				renderError("保存失败！");
			}
		}
	}

	/***
	 * 删除
	 * @throws Exception
	 */
	public void delete() throws Exception {
		String id = getPara("id");
		BizSyncDataTask role = BizSyncDataTask.dao.findById(id);
		if (role != null) {
			if (role.delete()) {
				renderSuccess("删除成功！");
				try {
					Db.delete("DELETE FROM " + BizSyncDataLog.TABLE_NAME +" O  WHERE O.TASKID='" + role.getId() + "' ");
				}catch (Exception e) {
				}
			} else {
				renderError("删除失败！");
			}
		} else {
			renderError("数据不存在,请刷新后再试!");
		}
	}

	public void listDataLog() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("limit", 10);
		String taskId = getPara("taskId");
		String keyword = getPara("keyword");

		String select = " select *  ";
		String sqlExceptSelect = " from " + BizSyncDataLog.TABLE_NAME + " o  where o.taskId='" + taskId + "' ";
		if (!StrKit.isBlank(keyword)) {
			sqlExceptSelect += " and o.name like '%" + keyword + "%'  ";
		}
		sqlExceptSelect += " order by  o.startTime  ";
		Page<BizSyncDataLog> page = BizSyncDataLog.dao.paginate(pageNumber, pageSize, select, sqlExceptSelect);
		renderLayuiPage(0, page.getList(), "", page.getTotalRow());
	}

}
